<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>window</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            width: 100%;
            height: 100%;
        }

        .imgScale {
            width: auto;
            height: auto;
            display: block;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: black;
        }

        .imgScale img {
            max-width: 100%;
            max-height: 100%;
            width: auto;
            height: auto;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate3d(-50%, -50%, 0);
            -webkit-transform: translate3d(-50%, -50%, 0);
        }
    </style>
    <script src="https://cdn.staticfile.org/pako/1.0.10/pako.min.js"></script>
    <script>
        function unzip(b64Data) {
            let strData = atob(b64Data);
            const charData = strData.split('').map(function (x) {
                return x.charCodeAt(0);
            });
            const binData = new Uint8Array(charData);
            const data = pako.inflate(binData);
            return new Uint16Array(data).reduce(
                function (data, byte) {
                    return data + String.fromCharCode(byte);
                },
                ''
            )
        }
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    </script>
</head>

<body>
    <div class="imgScale">
        <img src="" alt="" id="snapshot">
    </div>
    <script>
        var source = new EventSource("/sse?channel=" + getQueryString("channel"));
        source.onmessage = function (event) {
            let src = "data:image/png;base64, " + (event.data);
            document.getElementById("snapshot").setAttribute("src", src);
        };

        source.addEventListener('open', function (event) {
            console.log("push 服务已连接");
        }, false);

        source.addEventListener('close', function (event) {
            source.close();
            console.log("push 服务已断开");
        }, false);

        source.addEventListener('error', function (event) {
            if (event.readyState == EventSource.CLOSED) {
                console.log("连接关闭");
            } else {
                console.log(event);
            }
        }, false);
    </script>
</body>

</html>